package com.zzgele.qmzb.api.web;

import com.alibaba.boot.dubbo.annotation.DubboConsumer;
import com.zzgele.qmzb.api.core.Result;
import com.zzgele.qmzb.api.core.ResultGenerator;
import com.zzgele.qmzb.api.model.Gift;
import com.zzgele.qmzb.api.model.GiftLog;
import com.zzgele.qmzb.api.model.User;
import com.zzgele.qmzb.api.service.GiftLogService;
import com.zzgele.qmzb.api.service.GiftService;
import com.zzgele.qmzb.api.service.UserService;
import com.zzgele.qmzb.api.utils.CalculateUtil;
import com.zzgele.qmzb.api.utils.DateUtil;
import com.zzgele.qmzb.api.utils.LevelUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
* Created by Martin on 2017/08/04.
*/
@RestController
@RequestMapping("/gift")
public class GiftController {
    private final Logger logger = LoggerFactory.getLogger(GiftController.class);
    @DubboConsumer
    private GiftService giftService;
    @DubboConsumer
    private UserService userService;
    @DubboConsumer
    private GiftLogService giftLogService;
    @PostMapping("/words")
    public Result send(Long login_uid) {
        //弹幕1个钻价格
        int amount=1;
        User user = userService.findById(login_uid);
        BigDecimal userDiamond = user.getDiamond();
        BigDecimal moneyTotal=new BigDecimal(1);
        HashMap hashMap = new HashMap();
        hashMap.put("diamond", userDiamond.intValue());
        if(userDiamond.intValue()<1) {
            return ResultGenerator.genFailResult("余额不足", hashMap);
        }
        //扣钻石
        // add score
        user.setScoreUser(user.getScoreUser()+moneyTotal.intValue()*10);
        user.setDiamond(user.getDiamond().subtract(moneyTotal));
        //加花费钻
        user.setSpend(user.getSpend() + moneyTotal.intValue());
        int level= LevelUtil.getLevel(user.getScoreUser());
        user.setLevel(level);
        userService.update(user);
        //记录送礼日志
        GiftLog giftLog = new GiftLog();
        giftLog.setAmount(amount);
        giftLog.setCreateTime(DateUtil.getCurUnixTimeStampInt());
        giftLog.setDiamond(moneyTotal);
        giftLog.setGid(100);
        giftLog.setTuid(100L);
        giftLog.setUid(login_uid);
        giftLogService.save(giftLog);
        userDiamond = user.getDiamond();
        hashMap.put("level", user.getLevel());
        hashMap.put("ticket", user.getTicket());
        hashMap.put("diamond", userDiamond);
        return ResultGenerator.genSuccessResult("购买弹幕成功", hashMap);
    }
    @PostMapping("/send")
    public Result send(Long login_uid, Long to_uid, Integer gift_id, Integer amount) {

        if(login_uid==null||to_uid==null||gift_id==null){
            return ResultGenerator.genFailResult("参数错误");
        }
        User user = userService.findById(login_uid);
        BigDecimal userDiamond = user.getDiamond();

        HashMap hashMap = new HashMap();
        hashMap.put("diamond", userDiamond);


        User userTo = userService.findById(to_uid);
        if (userTo == null) {
            return ResultGenerator.genFailResult("收礼人不存在", hashMap);
        }

        Gift gift = giftService.findById(gift_id);
        if (gift == null) {
            return ResultGenerator.genFailResult("礼物异常", hashMap);
        }

        BigDecimal moneyTotal = CalculateUtil.getTotal(gift.getPrice(), amount);
        if(!CalculateUtil.isGreaterEqual(userDiamond, moneyTotal)) {
            return ResultGenerator.genFailResult("余额不足以支付", hashMap);
        }
        HashMap map;

        //加票
        map = new HashMap();
        map.put("change", moneyTotal);
        map.put("uid", to_uid);
        userService.setInc(map);

        //扣钻石
        // add score
        user.setScoreUser(user.getScoreUser()+moneyTotal.intValue()*10);
        user.setDiamond(user.getDiamond().subtract(moneyTotal));
        //加花费钻
        user.setSpend(user.getSpend() + moneyTotal.intValue());
        int level= LevelUtil.getLevel(user.getScoreUser());
        user.setLevel(level);
        userService.update(user);
        //记录送礼日志
        GiftLog giftLog = new GiftLog();
        giftLog.setAmount(amount);
        giftLog.setCreateTime(DateUtil.getCurUnixTimeStampInt());
        giftLog.setDiamond(moneyTotal);
        giftLog.setGid(gift_id);
        giftLog.setTuid(to_uid);
        giftLog.setUid(login_uid);
        giftLogService.save(giftLog);
        userDiamond = user.getDiamond();
        hashMap.put("level", user.getLevel());
        hashMap.put("ticket", user.getTicket());
        hashMap.put("diamond", userDiamond);
        return ResultGenerator.genSuccessResult("礼物赠送成功", hashMap);
    }
    @PostMapping("/getGiftList")
    public Result getVideoList() {
        Condition condition = new Condition(Gift.class);
        condition.setOrderByClause("sort DESC");
        List<Gift> list=giftService.findByCondition(condition);
        return ResultGenerator.genSuccessResult(list);
    }
}
